<?php

namespace app\stores\model\logs;

use app\common\model\CommonModel;



class MemberPlatfromMoneyLogsModel extends CommonModel{

    protected  $name = 'member_money_detail';

    protected  $pk   = 'id';
    /**
     * 模型写入前事件（钩子函数）
     */
    public static function onBeforeInsert($model)
    {
        $model->set("add_time", date('Y-m-d H:i:s'));
        $model->set("add_ip", request()->ip());
    }
    public function getPlatfromMoneyLogsList($where,$money_where,$order,$page=0,$limit=0){
        $query = $this->alias('ml')->join('shop s','ml.shop_id=s.shop_id')
            ->when(!empty($where['member_id']),function ($query) use ($where){
                $query->where(['ml.member_id'=>$where['member_id']]);
            })
            ->when(!empty($where['type']),function ($query) use ($where){
                $query->where('ml.type','=',$where['type']);
            })
            ->when(!empty($where['add_start_time']),function ($query) use ($where){
                $query->where('ml.add_time','>=',$where['add_start_time']);
            })
            ->when(!empty($where['add_end_time']),function ($query) use ($where){
                $query->where('ml.add_time','<=',$where['add_end_time']);
            })->field("ml.type,ml.info,ml.money,ml.give_money,ml.add_time,s.name as shop_name");
        $count = $query->count();
        $query = $query->order($order);
        if($page>0) $query = $query->page($page);
        if($limit>0) $query = $query->limit($limit);
        $list = $query->select()->toArray();
        $out_money = $this->where($money_where)->where(['type'=>-1])->sum('money');
        $out_money = sprintf("%.2f", $out_money);
        $income_money = $this->where($money_where)->where(['type'=>1])->sum('money');
        $income_money = sprintf("%.2f", $income_money);
        if(!empty($list)){
            foreach ($list as $k=>$v){

            }
        }
        return compact('count','list','out_money','income_money');
    }
}

